
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "RTDirectionalLight.h"
#include "RTMatrix44.h"

namespace RT
{
	/*Default constructor*/
	RTDirectionalLight::RTDirectionalLight(): m_direction(0, -1, 0), m_viewDirection(RTVector3f())
	{
		m_lightColor = RTColor(1, 1, 1, 1);
		m_type = eDirectionalLight;
	}

	/*Constructor*/
	RTDirectionalLight::RTDirectionalLight(const RTColor& color, const RTVector3f& direction)
	: m_direction(direction), m_viewDirection(RTVector3f())
	{
		m_lightColor = color;
		m_direction.normalize();
		m_type = eDirectionalLight;
	}

	/*Destructor*/
	RTDirectionalLight::~RTDirectionalLight(){}

	/*Return the color of the light*/
	RTColor RTDirectionalLight::computeColor(const RTRay& /*ray*/, F32 /*dist*/)const
	{
		return m_lightColor;
	}

	/*Calculate the shadow ray between a point and the light*/
	void RTDirectionalLight::computeShadowRay(const RTVector3f& point, RTRay& shadowRay, F32* distance)const
	{
		*distance = -1;

		//to avoid intersection with itself, move the origin along the direction
		shadowRay.setOrigin(point - m_viewDirection * 0.1f);
		shadowRay.setDirection(-m_viewDirection);
	}

	/*Apply the view matrix to the light*/
	void RTDirectionalLight::applyViewMatrix(const RTMatrix44& view)
	{

		//calculate the direction vector in view coordinates
		RTVector3f dirStart = RTVector3f(0, 0, 0) * view;//.inverse();
		RTVector3f dirEnd = m_direction * view;//.inverse();
		m_viewDirection = dirEnd - dirStart;
		m_viewDirection.normalize();
	}

	/*Set the direction of the Directional Light*/
	void RTDirectionalLight::setDirection(const RTVector3f& direction)
	{
		m_direction = direction;
		m_direction.normalize();
	}

}